<%@page import="com.liferay.portal.service.ListTypeServiceUtil"%>
<%@page import="com.liferay.portal.model.ListType"%>
<%@page import="com.liferay.portal.theme.ThemeDisplay"%>
<%@page import="com.crossriver.crgis.database.model.Contact_Information"%>
<%@ page import="com.crossriver.crgis.cofo.util.ApplicationCofOConstants" %>
<%@page import="com.liferay.portal.model.Country"%>
<%@page import="com.liferay.portal.service.CountryServiceUtil"%>
<%@page import="com.crossriver.crgis.database.service.persistence.Application_C_of_OUtil"%>
<%@ page import="com.crossriver.crgis.database.model.Application_C_of_O" %>
<%@ include file="/init.jsp"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:resourceURL var="ajaxCallResourceURL" />
<portlet:renderURL var="ApplicationCofO1">
		<portlet:param name="jspPage" value="/html/individualcofo/ApplicationCofO1.jsp" />
</portlet:renderURL>
<portlet:renderURL var="ApplicationCofO3">
		<portlet:param name="jspPage" value="/html/individualcofo/ApplicationCofO3.jsp" />
</portlet:renderURL>

<%
Contact_Information contactInfo =
		(Contact_Information)request.getAttribute(
					ApplicationCofOConstants.IND_CONTACT_SESSION_ATTR);
	
Application_C_of_O applicatn = 
	Application_C_of_OUtil.findByPrimaryKey(contactInfo.getApplicationId());
%>



	<liferay-portlet:actionURL name="saveAddressInformation" var="saveAddressInformation"></liferay-portlet:actionURL>
 <aui:form action="<%=saveAddressInformation.toString() %>" method="post" name="saveAddressInformation_fm">
    	 <% String app = applicatn.getApplication(); %>
 <div id="main">
	<%
	if("certification".equalsIgnoreCase(app)){
	%>
	<h1>Application for Certificate Of Occupancy</h1>
	<%
	}else if("re-certification".equalsIgnoreCase(app)){
	%>
	<h1>Application for Re-Certificate Of Occupancy</h1>
	<%
	}else {
	%>
	<h1>Application for State Land</h1>
	<%
	}
	%>
	
  <div class="content">
  
  	
		<liferay-ui:error key="house-no-mandatory" message="house-no-mandatory" />
		<liferay-ui:error key="address-is-mandatory" message="address-is-mandatory" />
		<liferay-ui:error key="city-is-mandatory" message="city-is-mandatory" />
		<liferay-ui:error key="country-is-mandatory" message="country-is-mandatory" />
		<liferay-ui:error key="state-is-mandatory" message="state-is-mandatory" />
		<liferay-ui:success key="address-information-save-success" message="address-information-save-success"></liferay-ui:success>
		
  	<table width="100%" border="0" cellpadding="0" cellspacing="0">
    	<tr>
     	<td width="15%" valign="top">
     		<aui:input name="applicationId" type="hidden" value="<%= contactInfo.getApplicationId() %>"></aui:input>
    		<aui:input name="buttonClicked" type="hidden" value=""></aui:input>
    		<table width="100%" id="steps" cellpadding="0" cellspacing="0">
       		<tr><td valign="middle" align="center">STEP 1</td></tr>
       		<tr><td valign="middle" align="center" class="current">STEP 2</td></tr>
	        <tr><td valign="middle" align="center">STEP 3</td></tr>
          <tr><td valign="middle" align="center">STEP 4</td></tr>
          <tr><td valign="middle" align="center">STEP 5</td></tr>
          <tr><td valign="middle" align="center">STEP 6</td></tr>
          <tr><td valign="middle" align="center">STEP 7</td></tr>
          <tr><td valign="middle" align="center">STEP 8</td></tr>                               
       	</table>
    	</td>
      <td width="85%" valign="top">
         	<table width="100%" id="mainform">
         		<tr>
         			<td class="title" colspan="3">Address Information</td>
            </tr>
            <tr>
            	<td colspan="3" align="right" >
	            	<hr>
	            	<aui:button type="button" value="Exit Application" onClick="exitApplication();"></aui:button>
	             	
	              
	             	<aui:button type="submit" value="Save" onClick="setButtonClicked('btnSave');" ></aui:button>
	            	<aui:button type="submit" value="Save and Continue" onClick="setButtonClicked('btnSaveAndContinue');" ></aui:button>
	             	<aui:button type="button" value="Cancel Application" onClick="confirmPost()"></aui:button>
              </td>
           	</tr>
         		<tr>
            	<td colspan="3">
	              <aui:input helpMessage="Enter your House No" label="House No<span class='mandatoryfld'>*</span>"
	              	value="<%= contactInfo.getHouseNo() %>"
	                name="houseNo" style="width:50px">
	             	</aui:input> 
             	</td>
         		</tr>
            <tr>
	        		<td colspan="3">
	        			<br>
                <aui:input label="Provide Address Line 1. It is Mandatory." name="addressLine1" 
	                value="<%= contactInfo.getAddressLine1() %>"
	                label="Address Line 1<span class='mandatoryfld'>*</span>" style="width:470px">
               	</aui:input>
	        		</td>
    				</tr>
	         	<tr>
			     		<td colspan="3">
			     			<br>
	             	<aui:input name="addressLine2" label="Address Line 2" 
	              	value="<%= contactInfo.getAddressLine2() %>"
	              	style="width:470px">
	             	</aui:input>
			     		</td>
			 		</tr>
					<tr>
						<td width="33%">
			        <br>
<%-- 			      <aui:column columnWidth="50"> --%>

<!-- 
<%= renderResponse.getNamespace() + "getStates(" + renderResponse.getNamespace()  + "country.value);" %>
 -->
                  	<aui:select name="country" label="Country<span class='mandatoryfld'>*</span>" style="width:130px"  	
 				  		helpMessage="" >
 				  		<option label="" value="" >  </option>
                  	
 				  		<%
 				  		String country = contactInfo.getCountry();
 				  		if (contactInfo== null  
 				  				|| contactInfo.getCountry() == null
 				  				|| contactInfo.getCountry().equalsIgnoreCase("") ){
 				  			
 				  		}else{
 				  			country =  contactInfo.getCountry();
 				  		}
 				  		
		                List<Country> countries = CountryServiceUtil.getCountries();
		                
		                for (Country _country : countries) {
		                %> 
		                  		<aui:option selected="<%= _country.getName().equalsIgnoreCase(country) %>" 
		                  		value="<%=_country.getName() %>"><%=_country.getName()  %></aui:option>
		 				<%
		                }
		 				%>
                 	</aui:select>				  	
<%--                  </aui:column> --%>
						</td>
 						<td width="33%">
				     	<br>
				     	
				     	<aui:input label="State <span class='mandatoryfld'>*</span>" name="state" value="<%= contactInfo.getState() %>"></aui:input>
				     	<%-- <%
					List<ListType> states = ListTypeServiceUtil.getListTypes("com.teqbridgeltd.liferay.model.state");
					
					%>
					
					<aui:select name="state" style="width:130px" onchange='<%= renderResponse.getNamespace() + "getLGA(" + renderResponse.getNamespace()  + "state.value);" %>'
						 label="State<span class='mandatoryfld'>*</span>" listType="dropdownlist" >
							<option value="">-Select State-</option>
				  	
				  	<%
				  	String stateOfOriginS =contactInfo.getState();
					if (null !=stateOfOriginS && stateOfOriginS !=""){
				  	for(int i=0; i<states.size(); i++){
				  		ListType _states = states.get(i);
				  	%>	
				  	<aui:option selected="<%=  _states.getName().equalsIgnoreCase(stateOfOriginS) %>" 
		                  		value="<%=_states.getName() %>"><%=_states.getName()  %></aui:option>
				  	<%
				  	}
					}
				  	%>
					</aui:select> --%>
				 		</td>
						<td width="33%">
							<br>
							
							<aui:input label="State <span class='mandatoryfld'>*</span>" name="city" value="<%= contactInfo.getState() %>"></aui:input>
				<%-- <%
					List<ListType> lga = ListTypeServiceUtil.getListTypes("com.teqbridgeltd.liferay.model.lga." + contactInfo.getState() );
					
					%>
					
					<aui:select name="city" style="width:130px" id="city"
						 label="Local Government<span class='mandatoryfld'>*</span>" listType="dropdownlist" >
							<option value="">-Select LGA-</option>
				  	
				  	<%
				  	String LGAS = contactInfo.getCity();
				  	for(int i=0; i<lga.size(); i++){
				  		ListType _lga = lga.get(i);
				  	%>	
				  	<aui:option selected="<%=  _lga.getName().equalsIgnoreCase(LGAS) %>" 
		                  		value="<%=_lga.getName() %>"><%=_lga.getName()  %></aui:option>
				  	<%
				  	}
				  	%>
					</aui:select> --%>
					
						

						</td>
					</tr>
          <tr>
	      		<td colspan="3">
	            <br>
	            <aui:input name="postalMailAddress" label="P. O. Box Address" 
	            	value="<%= contactInfo.getPoBox() %>"
	             	style="width:400px">
	           	</aui:input>
	      		</td>
  				</tr>
          <tr>
     		  	<td colspan="3">
            	<br>
              <aui:input name="otherInfomation" label="Other Contact Information"  type="textarea"
              	value="<%= contactInfo.getOtherInformation() %>"
              	onKeyDown="limitText(this.form.limitedtextarea,this.form.countdown,180);"
               	onKeyUp="limitText(this.form.limitedtextarea,this.form.countdown,180);"
               	style="width:400px">
             	</aui:input>
      			</td>
  				</tr>
          <tr>
         		<td colspan="3" align="right">
            	<hr>
           		<aui:button type="button" value="Exit Application" onClick="exitApplication();"></aui:button>
             	
              
             	<aui:button type="submit" value="Save" onClick="setButtonClicked('btnSave');" ></aui:button>
            	<aui:button type="submit" value="Save and Continue" onClick="setButtonClicked('btnSaveAndContinue');" ></aui:button>
             	<aui:button type="button" value="Cancel Application" onClick="confirmPost()"></aui:button>
          	</td>
         	</tr>
     		</table>
 			</td>
   		</tr>
    </table>
  </div>
</div>

	


  <script type="text/javascript">
	function setButtonClicked(id)
	{
	   // alert(id);
	   document.<portlet:namespace/>saveAddressInformation_fm.<portlet:namespace/>buttonClicked.value=id;
	  // alert(document.<portlet:namespace/>quick_add_account_fm.<portlet:namespace/>buttonClicked.value);
	    //document.getElementById('jspPage').value=id;
	}

	function selectOptions()
	{
	   // alert(id);
	   document.<portlet:namespace/>saveAddressInformation_fm.<portlet:namespace/>houseNo.value='<%= contactInfo.getHouseNo() %>';
	   document.<portlet:namespace/>saveAddressInformation_fm.<portlet:namespace/>addressline1.value='<%= contactInfo.getAddressLine1() %>';
	   document.<portlet:namespace/>saveAddressInformation_fm.<portlet:namespace/>addressline2='<%= contactInfo.getAddressLine2() %>';
	   document.<portlet:namespace/>saveAddressInformation_fm.<portlet:namespace/>state.value='<%= contactInfo.getState() %>';
	   document.<portlet:namespace/>saveAddressInformation_fm.<portlet:namespace/>country.value='<%= contactInfo.getCountry() %>';
	   document.<portlet:namespace/>saveAddressInformation_fm.<portlet:namespace/>city.value='<%= contactInfo.getLga() %>';
	   document.<portlet:namespace/>saveAddressInformation_fm.<portlet:namespace/>otherInfomation.value='<%= contactInfo.getOtherInformation() %>';
	 }

	selectOptions();
	
	</script>
</aui:form>

	<liferay-portlet:actionURL name="exitApplication" var="exitApplication"></liferay-portlet:actionURL>
	<form name="<portlet:namespace/>exitApplication_fm" method="post"	id="<portlet:namespace/>exitApplication_fm" action="<%=exitApplication.toString()%>"></form>
	<script type="text/javascript">
	<!--
	function exitApplication()
	{
		document.forms["<portlet:namespace/>exitApplication_fm"].submit();
	}
	//-->
	</script>
	
<script type="text/javascript">
<!--
	function confirmPost()
	{
	var agree=confirm("Cancelling the process will delete the record you have entered into the portal for this application. Do you want to proceed?");
	if (agree){
		document.forms["<portlet:namespace/>deleteApplication_fm"].submit();
		return true ;
	}
	else{
		return false ;
	}
	
	}
//-->
</script>

<!-- 
<portlet:namespace />
 -->
<aui:script>
		
	Liferay.provide(window,
		'getStates',
		function(cntrName) {
		var countryName = cntrName.value;
	var A = AUI();
			var url = '<%= ajaxCallResourceURL.toString() %>';
			alert('test>>' +countryName);
				A.io.request(
					url,
					{
						//data to be sent to server
						data: {
							<portlet:namespace />countryName: countryName,
							<portlet:namespace />queryType: 'states',
						},
						dataType: 'json',

						on: {
							failure: function() {
							},

							success: function(event, id, obj) {
								var instance = this;


								//JSON Data coming back from Server
								
								var message = instance.get('responseData');
								
								var stateId = message.stateId;
								
								
						document.getElementById('<portlet:namespace />city').options.length = 0;		
						document.getElementById('<portlet:namespace />state').options.length = 0;
						var oFieldSelect= document.getElementById('<portlet:namespace />state');
									var state = document.createElement('option');  
									state.appendChild(document.createTextNode("-select a state-"));
									oFieldSelect.appendChild(state);
							if (stateId>0){
								if (message) {
							 for( i=1; i<=stateId; i++){
								    var param = "param_" + i;
									//alert(param);
									var oFieldSelect= document.getElementById('<portlet:namespace />state');
									var state = document.createElement('option'); 
									
						
									//state.appendChild(document.createTextNode(message." + param + ")); 
									state.appendChild(document.createTextNode(message[param]));
									oFieldSelect.appendChild(state); 
									  
								  }
								}
								else {
									alert('no data back from server');
								}
								}
							else{
								var oFieldSelect= document.getElementById('<portlet:namespace />state');
									var state = document.createElement('option');  
									state.appendChild(document.createTextNode("-Not available-"));
									oFieldSelect.appendChild(state); 
								}
							}

						}
					}

				); //END of io Request

		},
		['aui-io']
	);  //End of Provide


Liferay.provide(window,
		'<portlet:namespace />getLGA',
		function(stateName) {
			var A = AUI();
			var url = '<%= ajaxCallResourceURL.toString() %>';
				A.io.request(
					url,
					{
						//data to be sent to server
						data: {
							<portlet:namespace />stateName: stateName,
							<portlet:namespace />queryType: 'LGA',
						},
						dataType: 'json',

						on: {
							failure: function() {
							},

							success: function(event, id, obj) {
								var instance = this;


								//JSON Data coming back from Server
								
								var message = instance.get('responseData');
								
								var stateId = message.stateId;
								
						document.getElementById('<portlet:namespace />city').options.length = 0;
						var oFieldSelect= document.getElementById('<portlet:namespace />city');
									var state = document.createElement('option');  
									state.appendChild(document.createTextNode("-select a LGA-"));
									oFieldSelect.appendChild(state);
							if (stateId>0){
								if (message) {
								  for( i=1; i<=stateId; i++){
								    var param = "param_" + i;
									//alert(param);
									var oFieldSelect= document.getElementById('<portlet:namespace />city');
									var state = document.createElement('option'); 
									
						
									//state.appendChild(document.createTextNode(message." + param + ")); 
									state.appendChild(document.createTextNode(message[param]));
									oFieldSelect.appendChild(state); 
									  
								  }
								}
								else {
									alert('no data back from server');
								}
								}
							else{
								var oFieldSelect= document.getElementById('<portlet:namespace />city');
									var state = document.createElement('option');  
									state.appendChild(document.createTextNode("-Not available-"));
									oFieldSelect.appendChild(state); 
								}
							}

						}
					}

				); //END of io Request

		},
		['aui-io']
	);  //End of Provide
</aui:script>
<liferay-portlet:actionURL name="deleteApplication" var="deleteApplication"></liferay-portlet:actionURL>
 <aui:form action="<%=deleteApplication.toString() %>" method="post" name="deleteApplication_fm">
  	<aui:input name="applicationId" type="hidden" value="<%= contactInfo.getApplicationId() %>"></aui:input>
 </aui:form>
 
 